package com.briup.mt.service.impl;

import com.briup.mt.bean.Category;
import com.briup.mt.bean.CategoryExample;
import com.briup.mt.dao.CategoryMapper;
import com.briup.mt.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CategoryServiceImpl implements ICategoryService {
    @Resource
private CategoryMapper categoryMapper;
    @Override
    public List<Category> findAll() {
        CategoryExample example=new CategoryExample();
        List<Category>list = categoryMapper.selectByExample(example);
        return list;
    }

    @Override
    public List<Category> query(String name) {
        CategoryExample example= new CategoryExample();
        if (!StringUtils.isEmpty(name)){
            example.createCriteria().andNameLike("%"+name+"%");
        }
        List<Category> list=categoryMapper.selectByExample(example);
        return  list;
    }
    @Override
    public void saveOrUpdate(Category category) {
        if (category.getId() == null) {
            categoryMapper.insert(category);
        } else {
            categoryMapper.updateByPrimaryKey(category);
        }
    }
    @Override
    public void deleteById(long id) {
categoryMapper.deleteByPrimaryKey(id);
    }
}
